#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "handle.h"

int main() {
    struct sockaddr_in serv_addr = {0};
    struct sockaddr_in clie_addr = {0};

    int socket_sc = socket(AF_INET, SOCK_STREAM, 0);

    serv_addr.sin_family = AF_INET;//设置tcp协议族
    serv_addr.sin_addr.s_addr = inet_addr("10.0.0.1");//设置ip地址
    serv_addr.sin_port = htons(80);//设置端口

    connect(socket_sc, (struct sockaddr*)&serv_addr, sizeof(serv_addr));//客户端connect函数请求发起连接。

    char *buffer = malloc(1024);
    memset(buffer, 0, 1024);//清空buffer
    sprintf(buffer,
            "GET /index.html HTTP/1.1\r\n"
		    "Host: h2\r\n"
            "Connection: Keep-Alive\r\n\r\n"
            );//添加一个开头
    write(socket_sc, buffer, strlen(buffer));//buffer写入socket文件描述符，发送请求

    char* str = malloc(5*1024*1024);
    memset(str, 0, 5*1024*1024);
    read(socket_sc, str, 1024);
    printf("Client recv:\n%s", str);//打印收到的信息

    close(socket_sc);

    return 0;
}